package org.ehe.finance.customer.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.idempotent.annotation.RepeatSubmit;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.finance.currency.domain.bo.ErpFinanceCurrencyBo;
import org.ehe.finance.customer.domain.ErpFinanceCustomer;
import org.ehe.finance.customer.domain.bo.ErpFinanceCustomerBo;
import org.ehe.finance.customer.domain.vo.ErpFinanceCustomerVo;
import org.ehe.finance.customer.service.ErpFinanceCustomerService;
import org.ehe.finance.subject.domain.bo.ErpFinanceAccountSubjectBo;
import org.ehe.finance.subject.domain.vo.ErpFinanceAccountSubjectVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 客户档案
 * @author :zhangnn
 * @className :ErpFinanceCustomerController
 * @description: TODO
 * @date 2025-08-27 16:39:23
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/erp/customer")
public class ErpFinanceCustomerController extends BaseController {

    private final ErpFinanceCustomerService erpCustomerService;

    /**
     * 查询客户信息列表
     */
    @SaCheckPermission("erp:customer:list")
    @GetMapping("/list")
    public TableDataInfo<ErpFinanceCustomerVo> list(ErpFinanceCustomerBo bo, PageQuery pageQuery) {
        return erpCustomerService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出客户信息列表
     */
    @SaCheckPermission("erp:customer:export")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpFinanceCustomerBo bo, HttpServletResponse response) {
        List<ErpFinanceCustomerVo> list = erpCustomerService.queryList(bo);
        ExcelUtil.exportExcel(list, "客户信息", ErpFinanceCustomerVo.class, response);
    }

    /**
     * 获取客户信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("erp:customer:query")
    @GetMapping("/{id}")
    public R<ErpFinanceCustomerVo> getInfo(@NotNull(message = "主键不能为空")
                                    @PathVariable Long id) {
        return R.ok(erpCustomerService.queryById(id));
    }

    /**
     * 新增客户信息
     */
    @SaCheckPermission("erp:customer:add")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @RepeatSubmit(interval = 2, timeUnit = TimeUnit.SECONDS, message = "{repeat.submit.message}")
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpFinanceCustomerBo bo) {
        return toAjax(erpCustomerService.insertByBo(bo));
    }

    /**
     * 修改客户信息
     */
    @SaCheckPermission("erp:customer:edit")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit(interval = 2, timeUnit = TimeUnit.SECONDS, message = "{repeat.submit.message}")
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpFinanceCustomerBo bo) {
        return toAjax(erpCustomerService.updateByBo(bo));
    }

    /**
     * 删除客户信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("erp:customer:remove")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(erpCustomerService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 客户状态修改
     */
    @SaCheckPermission("erp:customer:edit")
    @Log(title = "客户状态修改", businessType = BusinessType.UPDATE)
    @PostMapping("/changeCustomerStatus")
    public R<Void> changeCustomerStatus(@RequestBody ErpFinanceCustomerBo bo) {
        return toAjax(erpCustomerService.updateCustomerStatus(bo.getId(), bo.getStatus()));
    }

}
